package com.itheima.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.mapper.CheckGroupCheckItemMapper;
import com.itheima.mapper.CheckItemMapper;
import com.itheima.pojo.CheckItem;
import com.itheima.pojo.CheckgroupCheckitem;
import com.itheima.service.CheckItemService;
import com.mysql.jdbc.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Objects;

/**
 * @Description 检查项服务实现类
 * @Author makun
 * @Date 2023/9/13 15:33
 * @Version 1.0
 */
@Service
@Transactional
public class CheckItemServiceImpl extends ServiceImpl<CheckItemMapper, CheckItem> implements CheckItemService {
    @Autowired
    private CheckGroupCheckItemMapper checkGroupCheckItemMapper;

    @Override
    public List<CheckItem> getAll(String queryString) {
        // 查询条件
        LambdaQueryWrapper<CheckItem> checkItemLambdaQueryWrapper = new LambdaQueryWrapper<>();
        if (!StringUtils.isNullOrEmpty(queryString)) {
            checkItemLambdaQueryWrapper.like(CheckItem::getCode, queryString)
                    .or()
                    .like(CheckItem::getName, queryString);
        }
        return baseMapper.selectList(checkItemLambdaQueryWrapper);
    }

    @Override
    public Page<CheckItem> findPage(QueryPageBean queryPageBean) {
        // 查询条件
        LambdaQueryWrapper<CheckItem> checkItemLambdaQueryWrapper = new LambdaQueryWrapper<>();
        String queryString = queryPageBean.getQueryString();
        if (!StringUtils.isNullOrEmpty(queryString)) {
            checkItemLambdaQueryWrapper.like(CheckItem::getCode, queryString)
                    .or()
                    .like(CheckItem::getName, queryString);
        }
        // 分页条件
        Page<CheckItem> checkItemPage = new Page<>(queryPageBean.getCurrentPage(), queryPageBean.getPageSize());
        // 分页查询
        baseMapper.selectPage(checkItemPage, checkItemLambdaQueryWrapper);
        return checkItemPage;
    }

    @Override
    public boolean add(CheckItem checkItem) {
        return baseMapper.insert(checkItem) == 1;
    }

    @Override
    public Result findById(Integer id) {
        CheckItem checkItem = baseMapper.selectById(id);
        boolean flag = !Objects.isNull(checkItem);
        return new Result(flag, flag ? MessageConstant.QUERY_CHECKITEM_SUCCESS : MessageConstant.QUERY_CHECKITEM_FAIL, checkItem);
    }

    @Override
    public Result edit(CheckItem checkItem) {
        boolean flag = baseMapper.updateById(checkItem) == 1;
        return new Result(flag, flag ? MessageConstant.EDIT_CHECKITEM_SUCCESS : MessageConstant.EDIT_CHECKITEM_FAIL);
    }

    @Override
    public boolean delete(Integer id) {
        // 删除与检查组的关系
        checkGroupCheckItemMapper.delete(new LambdaQueryWrapper<CheckgroupCheckitem>().eq(CheckgroupCheckitem::getCheckitemId, id));
        // 删除检查项
        return baseMapper.deleteById(id) == 1;
    }
}
